<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>系统参数列表</title>
<jsp:include page="../inc.jsp"></jsp:include>
<script type="text/javascript">
	Ext.onReady(function() {
		Ext.QuickTips.init();
		createGrid();
		createLayout();
		createForm();
		form = Ext.create('sysParamForm');
	});

	var gridEl = 'repTime-grid';
	var grid, form, selModel, toolbar, searchFlds;
	var win;

	function createGrid() {
		var queryFlds = new Array();
		searchFlds = new Ext.util.MixedCollection();
		var fldTimeName = new Ext.form.TextField({
			name : 'keyWords',
			id : gridEl + '-keyWords',
			fieldLabel : '关键字',
			allowBlank : true,
			width : 200
		});

		searchFlds.add('keyWords', fldTimeName);
		grid = Ext.create('gridView', {
			remoteSort : true,
			gridId : gridEl,
			queryFlds : queryFlds,
			searchFlds : searchFlds,
			checkboxSelectionModel : true,
			entityClassName : 'com.rover.entity.system.SysParam',
			queryUrl : '../sysParamController/query.action',
			deleteUrl : '../sysParamController/delete.action',
			jsonId : 'id',
			recordPk : [ "id" ],
			pageSize : 5
		});
		selModel = grid.getSelectionModel();
		selModel.on('rowselect', onRowSelect);
	}

	// 选择某行
	function onRowSelect(selModel, rowIndex, record) {

	}

	// 刷新列表
	function reloadRecords() {
		grid.getStore().reload();
	}

	function createWindows() {
		if (win == null) {
			win = new Ext.Window(
					{
						title : '新增',
						iconCls : 'icon-win',
						autoHeight : true,
						width : 335,
						height : 200,
						buttonAlign : 'center',
						buttons : [
								{
									text : "保存",
									handler : function() {
										if (form.getForm().isValid()) {
											form
													.getForm()
													.submit(
															{
																url : '../sysParamController/save.action',
																scope : form,
																success : function(
																		form,
																		action) {
																	win.hide();
																	grid
																			.getStore()
																			.reload();
																},
																failure : function(
																		form,
																		action) {
																	if (action.result.message == undefined)
																		Ext.Msg
																				.alert(
																						'错误',
																						'网络连接失败.');
																	else
																		Ext.Msg
																				.alert(
																						'错误',
																						action.result.message);
																},
																waitMsg : '保存数据...'
															});
										} else {
											Ext.Msg.alert('提示', '表单含有无效或空数据');
										}
									},
									id : "btnSave"
								}, {
									text : "关闭",
									id : "btnClose",
									handler : function() {
										win.hide();
									}
								} ],
						layout : 'fit',
						plain : false,
						closable : false,
						modal : true,
						items : [ form ]
					});
		}
		win.show();
	}

	// 创建form表单
	function createForm() {
		Ext.define('sysParamForm', {
			extend : 'Ext.form.Panel',
			fields : new Ext.util.MixedCollection(),
			initComponent : function() {
				var me = this;
				var tn = 1;

				this.fields.add("id", new Ext.form.Hidden({
					name : "id",
					dataIndex : "id"
				}));
				this.fields.add("paramCode", new Ext.form.TextField({
					name : "paramCode",
					dataIndex : "paramCode",
					fieldLabel : "参数编码",
					allowBlank : false
				//错误信息的显示
				}));
				this.fields.add("paramVal", new Ext.form.TextField({
					name : "paramVal",
					dataIndex : "paramVal",
					fieldLabel : "参数值",
					allowBlank : false
				}));
				this.fields.add("description", new Ext.form.TextField({
					name : "description",
					dataIndex : "description",
					fieldLabel : "描述",
					allowBlank : true
				}));
				/*this.fields.add("paramType", new Ext.form.TextField({
					name : "paramType",
					dataIndex : "paramType",
					fieldLabel : "参数类型",
					allowBlank : true
				}));*/
				var sysParamStore = Ext.create('Ext.data.Store', {
					fields : [ 'itemText', 'itemValue' ],
					proxy : {
						type : 'ajax',
						method:'POST',
						 getMethod: function(){ return 'POST'; },//亮点，设置请求方式,默认为GET 
						url : '../codeValueController/getCodeValue.action',
						params : {  
				             codeName : '参数类型' 
				        },
						reader : {
							type : 'json',
							root : 'data'
						}
					},
					autoLoad : false
				});
				sysParamStore.load({params : {
					  codeName : '参数类型'
				}});

				this.fields.add("paramType", Ext.create(
						'Ext.form.field.ComboBox', {
							name : 'paramType',
							fieldLabel : '参数类型',
							valueField : 'itemValue',
							displayField : 'itemText',
							multiSelect : false,//多选
							allowBlank : false,// 不允许为空
							editable : false,// 是否允许输入
							forceSelection : true,// 必须选择一个选项
							blankText : '请选择',// 该项如果没有选择，则提示错误信息,
							labelWidth : 130,
							store : sysParamStore,
							queryMode : 'local',
							typeAhead : true
						}));
				this.items = [ {
					layout : "form",
					columnWidth : 1,
					labelAlign : "right",
					labelWidth : 100,
					items : [ this.fields.get("id"),
							this.fields.get("paramCode"),
							this.fields.get("paramVal"),
							this.fields.get("description"),
							this.fields.get("paramType") ]
				} ];
				this.frame = true;
				this.layout = 'fit';
				this.region = 'east';
				this.border = true;
				this.split = true;
				this.callParent(arguments);
			},
			onRender : function() {
				this.callParent(arguments);
			},
			newDataRecord : function() {
				this.fields.get("id").setValue('');
				this.fields.get("paramCode").setValue('');
				this.fields.get("paramVal").setValue('');
				this.fields.get("description").setValue('');
				this.fields.get("paramType").setValue('');
			}
		});
	}

	// 新建或修改表单成功提交后,重新加载其父节点
	function afterCommitFormSuccess(operation, recordData) {
		onWinHide();
		grid.getStore().reload();
	}

	function EditRecord() {
		var sm = grid.getSelectionModel();
		var records = sm.getSelection();

		if (records.length <= 0) {
			Ext.Msg.alert('错误', "未选中记录，不能修改！");
			return;
		}
		var data = records[0].data;
		form.getForm().findField('id').setValue(data.id);
		form.getForm().findField('paramCode').setValue(data.paramCode);
		form.getForm().findField('paramVal').setValue(data.paramVal);
		form.getForm().findField('description').setValue(data.description);
		form.getForm().findField('paramType').setValue(data.paramType);
		createWindows();
	}

	// 创建layout
	function createLayout() {
		toolbar = new Ext.Toolbar({
			items : [ {
				text : "新增",
				id : "tlb_NEW",
				handler : function() {
					form.newDataRecord();
					createWindows();
				}
			}, '-', {
				text : "编辑",
				id : "tlb_EDIT",
				handler : function() {
					EditRecord();
				}
			}, '-', {
				text : "删除",
				id : "tlb_DELETE",
				handler : function() {
					grid.deleteRecord();
				}
			}, '->', searchFlds.get("keyWords"), {
				text : "搜索",
				handler : function() {
					grid.load();
				}
			} ]
		});

		var viewport = new Ext.Viewport({
			layout : 'border',
			items : [ {
				layout : 'fit',
				region : 'north',
				border : false,
				items : [ toolbar ],
				height : 25
			}, {
				layout : 'border',
				region : 'center',
				border : false,
				items : [ grid ]
			} ]
		});

		viewport.doLayout();
	}
</script>
</head>
<body>
</body>
</html>